/* $Header: pi_util.c,v 2.2 01/10/02 10:28:00 przybyls Exp $ */

/**begin_proto**/
/* $Header: pi_util.c,v 2.2 01/10/02 10:28:00 przybyls Exp $ */
/**end_proto**/

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:                                                        */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE:                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <p_status.h>
#include <error.h>
#include <math.h>
#include <p_ui.h>

/** all of this mess to get one little proto out of sigpaath.h **/
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>
#include <sig_types.h>
#include <sigpath.h>

#include <i_op.h>

#include <method.h>
#include <mio.h>
#include <cmd_set.h>
#include <hostutil.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/**begin_proto**/

 /* constants used to define scaling factors between Keyboard and Flow */

#define DISPLAY_KPA_SCALE 1
#define DISPLAY_PSI_SCALE 10
#define DISPLAY_BAR_SCALE 100

#define DISPLAY_V_SCALE 1
#define DISPLAY_F_SCALE 10

#define DISPLAY_F_RATE_SCALE 100
#define DISPLAY_P_RATE_KPA_SCALE 10
#define DISPLAY_P_RATE_PSI_SCALE 100
#define DISPLAY_P_RATE_BAR_SCALE 1000

#define SPLIT_RATIO_UI_SCALE  10
#define SPLIT_RATIO_DC_SCALE  10000
#define SPLIT_RATIO_INTERNAL_SCALE 16384

#define COLUMN_DIAM_SCALING   1
#define COLUMN_LEN_SCALING    100
#define COLUMN_FILM_SCALING    100

#define MAX_SPLIT_RATIO       7500   /* not scaled */
#define MAX_SPLIT_RATIO_IU    ( MAX_SPLIT_RATIO * SPLIT_RATIO_INTERNAL_SCALE )
#define MIN_SPLIT_RATIO_IU    ( SPLIT_RATIO_INTERNAL_SCALE / 10)  /* .1 */

#define CALIB_GAIN_DC_SCALE   10000      /* xx.xx % */

#define MAX_COL_FINAL_FLOW    150

/**end_proto**/

#define MAX_FLOW_RATE         100
#define MAX_PRES_RATE_PSI     150
#define MAX_PRES_RATE_KPA    1034

/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 Round( INT32 num, INT8 frac_digits )

/**end_proto**/
{

   /* incoming num must be multiplied by 10 to keep significance */

   if (( frac_digits == 1 ) && ( num >  9990 )) num = ((num + 50 ) / 100 ) * 100;
   else                                         num = ((num + 5  ) / 10  ) * 10;

   return num;

}


/**begin_proto**/

U_INT32 GetInletMaxFlowIu ( pINLET_STATUS status, U_INT32 iu_scaling )

/**end_proto**/
{
     return QuadWordMultiplyDivide(status->Tcomp_Tables->Flow_Sensor.Parms.Max_Flow, iu_scaling, status->IU_Scaling );
}



/**begin_proto**/

INT32 GetRampMaxFlowRate ( void )

/**end_proto**/
{
     return  MAX_FLOW_RATE * DISPLAY_F_RATE_SCALE ;
}


/**begin_proto**/

UI_ERR ValidateFlowRate ( INT32 rate )

/**end_proto**/
{
     if ( rate <  0                    ) return PARAM_TOO_SMALL;
     if ( rate >  GetRampMaxFlowRate() ) return PARAM_TOO_LARGE;

     return OK;

}

/**begin_proto**/

INT32 GetRampMaxPresRate ( void )

/**end_proto**/
{
     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI: return  MAX_PRES_RATE_PSI * DISPLAY_P_RATE_PSI_SCALE;
        case DISPLAY_BAR: return  MAX_PRES_RATE_KPA * DISPLAY_P_RATE_BAR_SCALE / 100;
        case DISPLAY_KPA: return  MAX_PRES_RATE_KPA * DISPLAY_P_RATE_KPA_SCALE;
        default: return 0;
     }

}



/**begin_proto**/

UI_ERR ValidatePresRate ( INT32 rate )

/**end_proto**/
{
     if ( rate <   0                       ) return PARAM_TOO_SMALL;
     if ( rate >   GetRampMaxPresRate()    ) return PARAM_TOO_LARGE;

     return OK;
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/


/**begin_proto**/

INT32  GetMaxDetGasFlow( pDETECTOR_STATUS status, U_INT8 gas )

/**end_proto**/
{
     U_INT32 max_flow;

     switch ( gas )
     {
         case 1:  max_flow = status->Tcomp_Tables->Fuel.Frit.Parms.Max_Flow / status->Gas_1.IU_Scaling;
                  break;
         case 2:  max_flow = status->Tcomp_Tables->Util.Frit.Parms.Max_Flow / status->Gas_2.IU_Scaling;
                  break;
         case 3:  max_flow = status->Tcomp_Tables->Mug.Frit.Parms.Max_Flow  / status->Gas_3.IU_Scaling;
                  break;

         default: max_flow = 0;
     }

     return  DISPLAY_F_SCALE * max_flow;
}

/**begin_proto**/

INT32  GetMinDetGasFlow( pDETECTOR_STATUS status, U_INT8 gas )

/**end_proto**/
{
     U_INT32 min_flow;
     switch ( gas )
     {
         case 2:  min_flow = status->Min_Gas_2_Flow_Rate / status->Gas_2.IU_Scaling;
                  break;

         case 3:
                  if ( status->Config->Type == EPC_LDID )
                  {
                     min_flow = status->Min_Gas_2_Flow_Rate / status->Gas_2.IU_Scaling;
                  }
                  else
                  {
                     min_flow = 0;
                  }
                  break;

         default: min_flow =  0;
     }

     return  DISPLAY_F_SCALE * min_flow;
}

INT32  DcGetLDIDMinFlow (pDETECTOR_STATUS status)
{
   return (INT32) QuadWordMultiplyDivide ( status->Min_Gas_2_Flow_Rate, 1000, status->Gas_2.IU_Scaling );
}



/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 IamAdirtball(void)

/**end_proto**/
{

     return (BIT8)Inst_Config.Pneu.Dirt_Ball;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/
pINLET_STATUS GetFrontInletPtr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Frnt_Inlet;
}

/**begin_proto**/
pINLET_STATUS GetBackInletPtr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Back_Inlet;
}

/**begin_proto**/
pDETECTOR_STATUS GetFrontDetPtr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Frnt_Detector;
}

/**begin_proto**/
pDETECTOR_STATUS GetBackDetPtr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Back_Detector;
}

/**begin_proto**/
pAUX_STATUS GetAux3Ptr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Aux.Aux_3;
}

/**begin_proto**/
pAUX_STATUS GetAux4Ptr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Aux.Aux_4;
}

/**begin_proto**/
pAUX_STATUS GetAux5Ptr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Aux.Aux_5;
}

/**begin_proto**/
pCOLUMN_STATUS GetColumn1Ptr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Column_1;
}

/**begin_proto**/
pCOLUMN_STATUS GetColumn2Ptr(void)
/**end_proto**/
{
     return &Inst_Status.Pneu.Column_2;
}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/
BIT8 InletHasColumn( pINLET_STATUS status )
/**end_proto**/
{
      return (BIT8)status->Has_Column;
}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

PRES_UNITS GetPresUnits( void )

/**end_proto**/
{

   return Inst_Config.Pneu.Pressure_Display_Units;

}


/**begin_proto**/

UI_ERR ValidatePresUnits ( PRES_UNITS units )

/**end_proto**/
{

    switch ( units )
    {
       case DISPLAY_BAR:
       case DISPLAY_PSI:
       case DISPLAY_KPA:  return OK;
       default:           return INVALID_PARAM;
    }
}

/**begin_proto**/

UI_ERR SetPresUnits ( PRES_UNITS units )

/**end_proto**/
{

   UI_ERR return_value;

   return_value =  ValidatePresUnits( units );

   if ( return_value == OK )
   {
         INTERRUPTS_OFF

         if ( Inst_Config.Pneu.Pressure_Display_Units != units )
         {
             Parameter_Modified[0].bits.gc_param = 1;
             Parameter_Modified[1].bits.gc_param = 1;
             Parameter_Modified[(Cmd_Port==0)?1:0].bits.gc_param = 1;
         }

         Inst_Config.Pneu.Pressure_Display_Units = units;

         INTERRUPTS_ON

         SigPresUnitsChanged();
   }

   return return_value;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertPresIuToDisplay ( U_INT32 pres_iu , U_INT16 dcm2_per_iu )

/**end_proto**/
{

     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI: /* return PSI * 10  */
            return  Round((INT32)QuadWordMultiplyDivide (10 * pres_iu,
                                                    DISPLAY_PSI_SCALE*10*dcm2_per_iu,
                                                    689475 /* dcm2*10 -> PSI */), 1) / 10;
        case DISPLAY_BAR: /* return Bar * 1000 */
            return  Round((INT32)QuadWordMultiplyDivide ( 10 * pres_iu,
                                                    DISPLAY_BAR_SCALE*dcm2_per_iu,
                                                    1000000 /* dcm2 -> Bar */ ), 3) / 10;
        case DISPLAY_KPA:
            return  Round((INT32)QuadWordMultiplyDivide ( 10 * pres_iu,
                                                    DISPLAY_KPA_SCALE*dcm2_per_iu,
                                                    10000  /* dcm2 -> KPa */), 0) / 10;
        default: return 0;
     }

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT16 ConvertPresDisplayToIu ( INT32 pres , U_INT16 dcm2_per_iu )

/**end_proto**/
{

     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI: /* return PSI * 10  */
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      689475, /* dcm2*10 -> PSI */
                                                      DISPLAY_PSI_SCALE*10*dcm2_per_iu );
        case DISPLAY_BAR: /* return Bar * 1000 */
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      1000000, /* dcm2 -> Bar */
                                                      DISPLAY_BAR_SCALE*dcm2_per_iu );
        case DISPLAY_KPA:
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      10000,  /* dcm2 -> KPa */
                                                      DISPLAY_KPA_SCALE*dcm2_per_iu );
        default: return 0;
     }


}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertFlowIuToDisplay ( U_INT32 flow_iu , U_INT32 iu_scaling )

/**end_proto**/
{

    return Round((INT32) QuadWordMultiplyDivide ( 10 * flow_iu, DISPLAY_F_SCALE, iu_scaling ), 1 ) / 10;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertFlowDisplayToIu ( INT32 flow , U_INT32 iu_scaling )

/**end_proto**/
{

    return (INT32) QuadWordMultiplyDivide ( flow, iu_scaling, DISPLAY_F_SCALE );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertVelIuToDisplay ( U_INT32 vel_iu , U_INT32 iu_scaling )

/**end_proto**/
{

    return Round((INT32) QuadWordMultiplyDivide ( vel_iu, 10 * DISPLAY_V_SCALE, iu_scaling ),1)/10;

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertVelDisplayToIu ( INT32 vel , U_INT32 iu_scaling )

/**end_proto**/
{

    return (INT32) QuadWordMultiplyDivide ( vel, iu_scaling, DISPLAY_V_SCALE );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertPresRateIuToDisplay ( U_INT32 pres_iu , U_INT16 dcm2_per_iu )

/**end_proto**/
{

     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI: /* return PSI * 10  */
            return  (INT32)QuadWordMultiplyDivide ( pres_iu,
                                                    DISPLAY_P_RATE_PSI_SCALE*10*dcm2_per_iu,
                                                    689475 /* dcm2*10 -> PSI */);
        case DISPLAY_BAR: /* return Bar * 1000 */
            return  (INT32)QuadWordMultiplyDivide ( pres_iu,
                                                    DISPLAY_P_RATE_BAR_SCALE*dcm2_per_iu,
                                                    1000000 /* dcm2 -> Bar */ );
        case DISPLAY_KPA:
            return  (INT32)QuadWordMultiplyDivide ( pres_iu,
                                                    DISPLAY_P_RATE_KPA_SCALE*dcm2_per_iu,
                                                    10000  /* dcm2 -> KPa */);
        default: return 0;
     }

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT16 ConvertPresRateDisplayToIu ( INT32 pres , U_INT16 dcm2_per_iu )

/**end_proto**/
{

     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI: /* return PSI * 10  */
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      689475, /* dcm2*10 -> PSI */
                                                      DISPLAY_P_RATE_PSI_SCALE*10*dcm2_per_iu );
        case DISPLAY_BAR: /* return Bar * 1000 */
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      1000000, /* dcm2 -> Bar */
                                                      DISPLAY_P_RATE_BAR_SCALE*dcm2_per_iu );
        case DISPLAY_KPA:
            return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                                      10000,  /* dcm2 -> KPa */
                                                      DISPLAY_P_RATE_KPA_SCALE*dcm2_per_iu );
        default: return 0;
     }


}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertFlowRateIuToDisplay ( U_INT32 flow_iu , U_INT32 iu_scaling )

/**end_proto**/
{

    return (INT32) QuadWordMultiplyDivide ( flow_iu, DISPLAY_F_RATE_SCALE, iu_scaling );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertFlowRateDisplayToIu ( INT32 flow , U_INT32 iu_scaling )

/**end_proto**/
{

    return (INT32) QuadWordMultiplyDivide ( flow, iu_scaling, DISPLAY_F_RATE_SCALE );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertPresIuToDcu ( U_INT32 pres_iu , U_INT16 dcm2_per_iu )

/**end_proto**/
{
     /* Data Comm Units for pressure are dynes/cm^2 */

#if 0
   return  (INT32)QuadWordMultiplyDivide (pres_iu,
                                          1000*10*dcm2_per_iu,
                                          689475 /* dcm2*10 -> PSI */);
#else

    return  (INT32)QuadWordMultiplyDivide ( pres_iu, dcm2_per_iu, 1 );
#endif

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT16 ConvertPresDcuToIu ( INT32 pres , U_INT16 dcm2_per_iu )

/**end_proto**/
{
     /* Data Comm Units for pressure are dynes/cm^2 */

#if 0
    return  (U_INT16)QuadWordMultiplyDivide ( pres,
                                              689475, /* dcm2*10 -> PSI */
                                              1000*10*dcm2_per_iu );
#else
    return  (INT16)QuadWordMultiplyDivide ( pres, 1, dcm2_per_iu );
#endif

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertFlowIuToDcu ( U_INT32 flow_iu , U_INT32 iu_scaling )

/**end_proto**/
{
    /* Data Comm Units for flow are micro-Liters/min */

    /* internal representation is mL/min * iu_scaling */

    return (INT32) QuadWordMultiplyDivide ( flow_iu, 1000, iu_scaling );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertFlowDcuToIu ( INT32 flow , U_INT32 iu_scaling )

/**end_proto**/
{

    /* Data Comm Units for flow are micro-Liters/min */

    return (INT32) QuadWordMultiplyDivide ( flow, iu_scaling , 1000 );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertVelIuToDcu ( U_INT32 vel_iu , U_INT32 iu_scaling )

/**end_proto**/
{

    /* Data Comm Units for velocity are microns/sec */

    return (INT32) QuadWordMultiplyDivide ( vel_iu, 10000, iu_scaling );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertVelDcuToIu ( INT32 vel , U_INT32 iu_scaling )

/**end_proto**/
{

    /* Data Comm Units for velocity are microns/sec */

    return (INT32) QuadWordMultiplyDivide ( vel, iu_scaling, 10000 );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertPresRateIuToDcu ( U_INT32 pres_iu , U_INT16 dcm2_per_iu )

/**end_proto**/
{

     /* Data Comm Units for pressure are dynes/cm^2 per sec */

    return  (INT32)QuadWordMultiplyDivide ( pres_iu, dcm2_per_iu, 1 );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT16 ConvertPresRateDcuToIu ( INT32 pres , U_INT16 dcm2_per_iu )

/**end_proto**/
{
     /* Data Comm Units for pressure are dynes/cm^2 per sec */

    return  (INT32)QuadWordMultiplyDivide ( pres, 1, dcm2_per_iu );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

INT32 ConvertFlowRateIuToDcu ( U_INT32 flow_iu , U_INT32 iu_scaling )

/**end_proto**/
{

    /* Data Comm Units for flow are micro-Liters/sec2 */

    return (INT32) QuadWordMultiplyDivide ( flow_iu, 1000, iu_scaling );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

U_INT32 ConvertFlowRateDcuToIu ( INT32 flow , U_INT32 iu_scaling )

/**end_proto**/
{

    /* Data Comm Units for flow are micro-Liters/sec2 */

    return (INT32) QuadWordMultiplyDivide ( flow, iu_scaling, 1000 );

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

#define PRES_UNITS_CONVERTION  100

U_INT32 GetPresUnitsConversion()

/**end_proto**/
{

     switch (Inst_Config.Pneu.Pressure_Display_Units)
     {
        case DISPLAY_PSI:  return 689475;
        case DISPLAY_BAR:  return 1000000;
        case DISPLAY_KPA:  return 1000000;/*revisit  this does not seem right */
        default: return 1;
     }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void BeginMethodInstall ()

/**end_proto**/
{
    Inst_Status.Pneu.Installing_Method = TRUE;
}

/**begin_proto**/

void EndMethodInstall ()

/**end_proto**/
{
    Inst_Status.Pneu.Installing_Method = FALSE;
}

/**begin_proto**/

BIT8 DoingMethodInstall ()

/**end_proto**/
{
    return Inst_Status.Pneu.Installing_Method;
}

/**begin_proto**/

UI_ERR ZeroAllPresSensors( )

/**end_proto**/
{

     UI_ERR error;
     BIT8   all_ok;

     all_ok = TRUE;

     if ( EpcInlet( pFRONT_INLET ))
     {
         error = SetInletCalPresZero( pFRONT_INLET );
         all_ok = all_ok && (error == OK);
     }

     if ( EpcInlet( pBACK_INLET ))
     {
        error = SetInletCalPresZero( pBACK_INLET  );
        all_ok = all_ok && (error == OK);
     }

     if ( EpcDetector( pFRONT_DETECTOR ))
     {
        if (!DetIsTCDorAnyECD( pFRONT_DETECTOR ) )
        {
           error = SetDetCalFlowZero( pFRONT_DETECTOR, 1 );
           all_ok = all_ok && (error == OK);
        }

        if (!DetIsAnyUECD( pFRONT_DETECTOR ) )
        {
        error = SetDetCalFlowZero( pFRONT_DETECTOR, 2 );
        all_ok = all_ok && (error == OK);
        }

        error = SetDetCalFlowZero( pFRONT_DETECTOR, 3 );
        all_ok = all_ok && (error == OK);
     }

     if ( EpcDetector( pBACK_DETECTOR ))
     {
        if (!DetIsTCDorAnyECD( pBACK_DETECTOR ) )
        {
           error = SetDetCalFlowZero( pBACK_DETECTOR, 1 );
           all_ok = all_ok && (error == OK);
        }

        if (!DetIsAnyUECD( pBACK_DETECTOR ) )
        {
           error = SetDetCalFlowZero( pBACK_DETECTOR, 2 );
           all_ok = all_ok && (error == OK);
        }

        error = SetDetCalFlowZero( pBACK_DETECTOR, 3 );
        all_ok = all_ok && (error == OK);

     }

     if ( PneuAuxPresent() )
     {
        error = SetAuxCalPresZero( &Inst_Status.Pneu.Aux.Aux_3 );
        all_ok = all_ok && (error == OK);

        error = SetAuxCalPresZero( &Inst_Status.Pneu.Aux.Aux_4 );
        all_ok = all_ok && (error == OK);

        error = SetAuxCalPresZero( &Inst_Status.Pneu.Aux.Aux_5 );
        all_ok = all_ok && (error == OK);

     }

     if ( !all_ok ) error = TEST_FAILED;

     return OK;
}

